/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.web.user;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.media.entity.HkSystemAreaFreight;
import com.thinkgem.jeesite.modules.hk.media.entity.HkSystemFreight;
import com.thinkgem.jeesite.modules.hk.media.service.HkSystemFreightService;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductAreaFreight;
import com.thinkgem.jeesite.modules.hk.sys.entity.user.HkUserProductFreight;
import com.thinkgem.jeesite.modules.hk.sys.service.user.HkUserProductFreightService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.*;

/**
 * 用户产品运费模板Controller
 *
 * @author 罗天文
 * @version 2018-06-20
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/user/hkUserProductFreight")
public class HkUserProductFreightController extends BaseController {

    @Autowired
    private HkUserProductFreightService hkUserProductFreightService;

    @Autowired
    private HkSystemFreightService hkSystemFreightService;

    @ModelAttribute
    public HkUserProductFreight get(@RequestParam(required = false) String id) {
        HkUserProductFreight entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkUserProductFreightService.get(id);
        }
        if (entity == null) {
            entity = new HkUserProductFreight();
        }
        return entity;
    }

    @RequiresPermissions("sys:user:hkUserProductFreight:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkUserProductFreight hkUserProductFreight, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {

        if (StringUtils.isBlank(UserUtils.getShopId())) {
            addMessage(redirectAttributes, "登录超时，请重新登录");
            return "redirect:" + Global.getFrontPath();
        }
        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkUserProductFreight.setShop(hkUserShop);
        Page<HkUserProductFreight> page = hkUserProductFreightService.findPage(new Page<HkUserProductFreight>(request, response), hkUserProductFreight);
        List<HkUserProductFreight> list = Lists.newArrayList();
        list = page.getList();
        for (HkUserProductFreight hk : list) {
            String[] sarr = hk.getProvinceId().split(",");
            List<String> pname = hkUserProductFreightService.getProNameByPid(sarr);
            if (pname.size() > 0) {
                hk.setProvinceName(pname.toString());
            }
        }
        page.setList(list);
        model.addAttribute("page", page);
        return "hk/sys/user/hkUserProductFreightList";
    }


    @RequiresPermissions("sys:user:hkUserProductFreight:view")
    @RequestMapping(value = "form")
    public String form(HkUserProductFreight hkUserProductFreight, Model model) {

        if (StringUtils.isNotBlank(hkUserProductFreight.getProvinceId())) {
            String[] sarr = hkUserProductFreight.getProvinceId().split(",");
            List<String> pname = hkUserProductFreightService.getProNameByPid(sarr);
            if (pname.size() > 0) {
                hkUserProductFreight.setProvinceName(pname.toString());
            }

            List<HkUserProductAreaFreight> hpList = hkUserProductFreight.getHkUserProductAreaFreightList();
            for (HkUserProductAreaFreight ha : hpList) {
                if (StringUtils.isNotBlank(ha.getProvinceId())) {
                    String[] arr = ha.getProvinceId().split(",");
                    List<String> sysPname = hkUserProductFreightService.getProNameByPid(arr);
                    if (sysPname.size() > 0) {
                        ha.setProvinceName(sysPname.toString());
                        hkUserProductFreight.setHkUserProductAreaFreightList(hpList);
                    }
                }
            }
        }

        model.addAttribute("hkUserProductFreight", hkUserProductFreight);
        return "hk/sys/user/hkUserProductFreightForm";
    }


    @RequiresPermissions("sys:user:hkUserProductFreight:edit")
    @RequestMapping(value = "save")
    public String save(HkUserProductFreight hkUserProductFreight, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, hkUserProductFreight)) {
            return form(hkUserProductFreight, model);
        }

        if (StringUtils.isBlank(UserUtils.getShopId())) {
            addMessage(redirectAttributes, "登录超时，请重新登录");
            return "redirect:" + Global.getFrontPath();
        }


        String[] str = hkUserProductFreight.getProvinceId().split(",");
        List<String> strsToList = Arrays.asList(str);


        List<HkUserProductAreaFreight> list  = hkUserProductFreight.getHkUserProductAreaFreightList();
        List<String> subList = Lists.newArrayList();
        for (HkUserProductAreaFreight s : list){
            String[] subStr = s.getProvinceId().split(",");
            subList.addAll(Arrays.asList(subStr));
        }

        List<HkSystemAreaFreight> sysList = hkUserProductFreight.getHkSystemAreaFreightList();
        List<String> strList = Lists.newArrayList();
        List<String> sss = Lists.newArrayList();
        if (sysList.size() > 0) {
            for (HkSystemAreaFreight hs : sysList) {
                String[] s = hs.getProvinceId().split(",");
                for (String ss : s) {
                    if (ss.length() <= 10) {
                        sss.add(ss);
                    }
                }
                strList.addAll(sss);
            }
        }

        //判断系统模板和所选省是否有重复
       /* List<HkSystemFreight> listHkSys = hkSystemFreightService.findList(new HkSystemFreight());
        List<String> ListHsf = Lists.newArrayList();
        sss = Lists.newArrayList();
        if (listHkSys.size() > 0) {
            for (HkSystemFreight hf : listHkSys) {
                if (StringUtils.isNotBlank(hf.getProvinceId())) {
                    String[] s = hf.getProvinceId().split(",");
                    for (String ss : s) {
                        if (ss.length() <= 10) {
                            sss.add(ss);
                        }
                    }
                    ListHsf.addAll(sss);
                }
            }
        }
        if (false == Collections.disjoint(ListHsf, strList) || false == Collections.disjoint(ListHsf, strsToList) || false == Collections.disjoint(ListHsf, subList)) {
            addMessage(model, "所选省份已存在模板中，不可重复选择。");
            return form(hkUserProductFreight, model);
        }*/


        //判断所选省是否存已存在用户模板中
        /*List<HkUserProductFreight> hpList = hkUserProductFreightService.findList(hkUserProductFreight);
        List<String> ListHp = Lists.newArrayList();
        sss = Lists.newArrayList();
        if (hpList.size() > 0) {
            for (HkUserProductFreight hp : hpList) {
                if (StringUtils.isNotBlank(hp.getProvinceId())) {
                    String[] s = hp.getProvinceId().split(",");
                    for (String ss : s) {
                        if (ss.length() <= 10) {
                            sss.add(ss);
                        }
                    }
                    ListHp.addAll(sss);
                }
            }
        }
        if (false == Collections.disjoint(ListHp, strList) || false == Collections.disjoint(ListHp, strsToList) || false == Collections.disjoint(ListHp, subList)) {
            addMessage(model, "所选省份已存在模板中，不可重复选择。");
            return form(hkUserProductFreight, model);
        }
*/


        //判断子集中是否有重复省
        if (subList.size() != new HashSet<String>(subList).size()) {
            addMessage(model, "所选省份已存在模板中，不可重复选择。");
            return form(hkUserProductFreight, model);
        }

        //判断所选省是否有重复
        if (false == Collections.disjoint(strsToList, strList) || false == Collections.disjoint(strsToList, subList)) {
            addMessage(model, "所选省份已存在模板中，不可重复选择。");
            return form(hkUserProductFreight, model);
        }

        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkUserProductFreight.setShop(hkUserShop);
        //是否除之外  否
        hkUserProductFreight.setIsexcept("0");
        //是否系统模板  用户自定义为否
        hkUserProductFreight.setIssystem("0");
        hkUserProductFreightService.save(hkUserProductFreight);
        addMessage(redirectAttributes, "保存用户产品运费模板成功");
        return "redirect:" + Global.getAdminPath() + "/sys/user/hkUserProductFreight/?repage";
    }


    @RequiresPermissions("sys:user:hkUserProductFreight:edit")
    @RequestMapping(value = "delete")
    public String delete(HkUserProductFreight hkUserProductFreight, RedirectAttributes redirectAttributes) {
        hkUserProductFreightService.delete(hkUserProductFreight);
        addMessage(redirectAttributes, "删除用户产品运费模板成功");
        return "redirect:" + Global.getAdminPath() + "/sys/user/hkUserProductFreight/?repage";
    }


    @RequiresPermissions("sys:user:hkUserProductFreight:view")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        HkUserProductFreight hkUserProductFreight = new HkUserProductFreight();
        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkUserProductFreight.setShop(hkUserShop);
        List<HkUserProductFreight> list = hkUserProductFreightService.findList(hkUserProductFreight);
        for (int i = 0; i < list.size(); i++) {
            HkUserProductFreight e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()))) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", "0");
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }


}